package ttelectronic.mobileholder.dataaccess;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MHDataBaseWrapper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "MobileHolder.db";
	private static final int DATABASE_VERSION = 1;

	public static final String PIN_TABLE = "PIN_TABLE";
	public static final String PIN_LEVEL = "PIN_LEVEL";
	public static final String PIN_ID = "PIN_ID";
	public static final String TEM_LEVEL = "TEM_LEVEL";

	private static final String DATABASE_CREATE = "create table " + PIN_TABLE
			+ "(" + PIN_ID + " integer primary key, " + PIN_LEVEL
			+ " integer not null, " + TEM_LEVEL + " integer not null );";

	public MHDataBaseWrapper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	public void addPin(MHPinObject pin) {
		SQLiteDatabase db = this.getWritableDatabase();

		// 2. create ContentValues to add key "column"/value
		ContentValues values = new ContentValues();
		values.put(PIN_ID, pin.getPinId()); // get title
		values.put(PIN_LEVEL, pin.getPinLevel()); // get author
		values.put(TEM_LEVEL, pin.getTemLevel());
		// 3. insert
		db.insert(PIN_TABLE, // table
				null, // nullColumnHack
				values); // key/value -> keys = column names/ values = column
		db.close();
	}

	public MHPinObject getPin() {
		// 1. get reference to readable DB
		SQLiteDatabase db = this.getReadableDatabase();

		String[] pinLevel = null;
		// 2. build query
		Cursor cursor = db.query(PIN_TABLE, // a. table
				pinLevel, // b. column names
				null,// " PIN_ID = 1", // c. selections
				null, // d. selections args
				null, // e. group by
				null, // f. having
				null, // g. order by
				null); // h. limit

		// 3. if we got results get the first one
		if (cursor != null)
			cursor.moveToFirst();

		// 4. build book object
		MHPinObject pin = new MHPinObject(
				Integer.parseInt(cursor.getString(0)), Integer.parseInt(cursor
						.getString(1)), Integer.parseInt(cursor.getString(2)));
		return pin;
	}

	public int updateBook(MHPinObject pin) {

		// 1. get reference to writable DB
		SQLiteDatabase db = this.getWritableDatabase();

		// 2. create ContentValues to add key "column"/value
		ContentValues values = new ContentValues();
		values.put(PIN_ID, pin.getPinId()); // get title
		values.put(PIN_LEVEL, pin.getPinLevel()); // get author
		values.put(TEM_LEVEL, pin.getTemLevel());
		// 3. updating row
		int i = db.update(PIN_TABLE, // table
				values, // column/value
				PIN_ID + " = ?", // selections
				new String[] { String.valueOf(pin.getPinId()) }); // selection
																	// args

		// 4. close
		db.close();
		return i;

	}
}
